package com.rd.student.dashuju.zhengchengxiang.July13th.Test;

import java.util.*;

public class RemoveUtils {
    public static void remove(LinkedList list) {
//        移除小组成员
//        移除已有的成员信息 通过名字移除 或者通过学号移除
//        如果成员所在的班级只有一人 暂定不可移除 返回提示信息
//        如果此成员不存在 返回处理失败，此成员不存在
        HashMap hashMap = new HashMap();

        for (int i = 0; i < list.size(); i++) {
            Student student = (Student) list.get(i);
            int key = student.getClasses();

            if (!hashMap.containsKey(key)) {
                hashMap.put(key, 1);
            } else {
                int value = (int) hashMap.get(key);
                hashMap.put(key, value + 1);
            }
        }
        info(hashMap);

        boolean memberRemoved = false;
        Scanner scanner = new Scanner(System.in);
        int id = scanner.nextInt();
        for (int i = 0; i < list.size(); i++) {
            Student student = (Student) list.get(i);
            if (student.getId() == id) {
                int key = student.getClasses();
                int value = (int) hashMap.get(key);

                if (value > 1) {
                    list.remove(i);
                    hashMap.put(key, value - 1);
                    memberRemoved = true;
                    System.out.println("删除成功！");
                    break;
                } else {
                    System.out.println("删除失败，班级只有一人，暂定不可移除");
                    memberRemoved = true;
                    break;
                }
            }
        }

        if (!memberRemoved) {
            System.out.println("删除失败，处理失败，此成员不存在");
        }
    }

    public static void info(HashMap hashMap) {
        Set set = hashMap.keySet();
        Iterator iterator = set.iterator();
        while (iterator.hasNext()) {
            Object key = iterator.next();
            Object value = hashMap.get(key);
            System.out.println(key + "班： " + value + "人");
        }
    }
}
